package com.lifu.yygh.cmn.controller;

import com.lifu.yygh.cmn.service.DictService;
import com.lifu.yygh.common.result.Result;
import com.lifu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description
 * @Author lifu
 * @Date 2021/5/8 16:53
 */
@Api(tags = "数据字典接口")
@RestController
@RequestMapping(value = "/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;

    //根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping(value = "/findByParentId/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }
    //根据dictCode获取下级节点
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
         List<Dict> list = dictService.findByDictCode(dictCode);
         return Result.ok(list);
    }
    //导出数据字典
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportDictData(response);
    }
    //导入数据字典
    //得加上@RequestParam注解，不然容易空指针异常
    @PostMapping(value = "/importData")
    public Result importData(@RequestParam(value = "excel") MultipartFile multipartFile){
        dictService.importDict(multipartFile);
        return Result.ok();
    }
    //根据dictcode和value查询
    @GetMapping(value = "/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode, @PathVariable String value){
        String dictName = dictService.getDictName(dictCode,value);
        return dictName;
    }
    //根据value查询
    @GetMapping(value = "/getName/{value}")
    public String getName(@PathVariable String value){
        String dictName = dictService.getDictName("",value);
        return dictName;
    }

}
